﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgNetComponentsDx.Options;

namespace BuildHelper.Work.Tools.Forms
{
    public partial class WyborCRa : XtraForm
    {
        private WyborCRa()
        {
            InitializeComponent();
        }
        
        private void gvMain_DoubleClick(object sender, EventArgs e)
        {
            btnOK_Click(btnOK, EventArgs.Empty);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            object obj = gvMain.GetFocusedRow();
            if (obj != null && obj is BuildHelper.GlobalTools.SoftDevTools.CRInfo)
            {
                Info = obj as BuildHelper.GlobalTools.SoftDevTools.CRInfo;
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void LoadCRs()
        {
            Cursor.Current = Cursors.WaitCursor;
            this.gcMain.DataSource = BuildHelper.GlobalTools.SoftDevTools.AllCrs;
            Cursor.Current = Cursors.Default;
        }

        BuildHelper.GlobalTools.SoftDevTools.CRInfo Info
        {
            get;
            set;
        }

        public static BuildHelper.GlobalTools.SoftDevTools.CRInfo ShowCRDialog(bool allowAll)
        {
            BuildHelper.GlobalTools.SoftDevTools.CRInfo info = null;
            using (WyborCRa f = new WyborCRa())
            {
                f.ceHistoria.Checked = OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Work_Group.Tortoise_Group.Commit.DodajHistorie);
                f.btnAll.Visible = allowAll;
                f.LoadCRs();
                if (f.ShowDialog() == DialogResult.OK)
                    info = f.Info;
            }
            return info;
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            BuildHelper.GlobalTools.SoftDevTools.GetCRInfo(false, ceHistoria.Checked, true, BuildHelper.GlobalTools.SoftDevTools.GetInfoFromSDForHistory);
            LoadCRs();
            this.Cursor = Cursors.Default;
        }

        private void btnGoTo_Click(object sender, EventArgs e)
        {
            object obj = gvMain.GetFocusedRow();
            if (obj != null && obj is BuildHelper.GlobalTools.SoftDevTools.CRInfo)
            {
                GlobalTools.SoftDevTools.CRInfo Info = obj as BuildHelper.GlobalTools.SoftDevTools.CRInfo;
                GlobalTools.SoftDevTools.NawigateTo(GlobalTools.Options.CRPrefix + Info.Numer);
            }
        }

        private void btnPusteInfo_Click(object sender, EventArgs e)
        {
            Info = new GlobalTools.SoftDevTools.CRInfo();
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnHistory_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            BuildHelper.GlobalTools.SoftDevTools.GetCRInfo(false, true, false, BuildHelper.GlobalTools.SoftDevTools.GetInfoFromSDForHistory);
            LoadCRs();
            this.Cursor = Cursors.Default;
        }

        private void rep_gridColumn6_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
             object obj = gvMain.GetFocusedRow();
             if (obj != null && obj is BuildHelper.GlobalTools.SoftDevTools.CRInfo)
             {
                 BuildHelper.GlobalTools.SoftDevTools.CRInfo i = obj as BuildHelper.GlobalTools.SoftDevTools.CRInfo;
                 BuildHelper.GlobalTools.SoftDevTools.GetCRIngoFromWebSD(i);
                 this.gcMain.RefreshDataSource();
             }
        }
        
        
    }
}
